<?php
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);
header("Content-Type: text/html;charset=utf-8");
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
mysql_connect("localhost","root","root");
@mysql_select_db(comds) or die("Unable to select database");

function exportToCsv($csv_data, $filename = 'export.csv') {
	$csv_terminated = "\n";
	$csv_separator = ",";
	$csv_enclosed = '"';
	$csv_escaped = "\\";
	// Gets the data from the database
	$schema_insert = '';
	$out = '';
	//  $destination_folder = './file/';
	// Format the data
	foreach ($csv_data as $row) {
		$schema_insert = '';
		$fields_cnt = count($row);
		//printr($row);
		$tmp_str = '';
		foreach ($row as $v) {
			$tmp_str .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $v) . $csv_enclosed . $csv_separator;
		} // end for

		$tmp_str = substr($tmp_str, 0, -1);
		$schema_insert .= $tmp_str;

		$out .= $schema_insert;
		$out .= $csv_terminated;
	} // end while
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Content-Length: " . strlen($out));
	header("Content-type: text/x-csv");
	// header("Content-Disposition:filename=$filename");
	header('Content-Disposition:inline;filename="'.$filename.'"');   //auto
	echo $out;
}
$dateTime = date("Y-m-d",strtotime("-1 day"));

$results = mysql_query("select TotalExp,FirstLoginTime,TotalDollorGet,TodayGameTime,TodayTCrystalLose,Crystal,TodayGoldGet,TodayGoldLose,Cracked,WeaponList,TodayExp,TodayIapInfo,Gold,Level,GameActiveTime,TotalGoldGet,UILastId,HireoutSelfTimes,TotalGameTime,TodayTCrystalGet,deviceid,gamename,whatday,createdate from comds where whatday = '$dateTime%'");
//$count = mysql_fetch_row($results); {
// $showtime = date("Y-m-d");
//echo "$showtime ???:" . $count[0] . "<br>";
//}
$csv_data = array(array('TotalExp', 'FirstLoginTime','TotalDollorGet','TodayGameTime','TodayTCrystalLose','Crystal','TodayGoldGet','TodayGoldLose','Cracked','WeaponList','TodayExp','TodayIapInfo','Gold','Level','GameActiveTime','TotalGoldGet','UILastId','HireoutSelfTimes','TotalGameTime','TodayTCrystalGet','deviceid','gamename','whatday','createdate'));
while($count = mysql_fetch_array($results))
{
	// var_dump($count);

	array_push($csv_data, array($count[TotalExp],$count[FirstLoginTime],$count[TotalDollorGet],$count[TodayGameTime],$count[TodayTCrystalLose],$count[Crystal],$count[TodayGoldGet],$count[TodayGoldLose],$count[Cracked],$count[WeaponList],$count[TodayExp],$count[TodayIapInfo],$count[Gold],$count[Level],$count[GameActiveTime],$count[TotalGoldGet],$count[UILastId],$count[HireoutSelfTimes],$count[TotalGameTime],$count[TodayTCrystalGet],$count[deviceid],$count[gamename],$count[whatday],$count[createdate]));

}
exportToCsv($csv_data, "$dateTime.csv");

?>